<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery" />
        </el-tooltip>
        <el-tooltip class="item" effect="dark" :content="showSearch ? '隐藏搜索' : '显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch" />
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="用户等级" align="center" prop="memberLevel" :formatter="memberLevelFormat" />
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" align="center" prop="registerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-show="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleFrozen(scope.row)"
            v-hasPermi="['user:user:frozen']"
          >冻结账户</el-button>
          <el-button
            v-show="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleThaw(scope.row)"
            v-hasPermi="['user:user:thaw']"
          >解冻账户</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
            v-hasPermi="['user:user:query']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户详情对话框 -->
    <el-dialog title="用户详情" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="8">
            <el-form-item label="用户ID" prop="id">
              {{form.id}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户昵称" prop="nickname">
              {{form.nickname}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="出生日期" prop="birthday">
              {{ parseTime(form.birthday,'{y}-{m}-{d}') }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="用户账号" prop="username">
              {{form.username}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户手机号1" prop="mobile1">
              {{form.mobile1}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户手机号2" prop="mobile2">
              {{form.mobile2}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="会员号" prop="memberNum">
              {{form.memberNum}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="会员等级" prop="memberLevel">
              {{memberLevelFormat(form)}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="账户状态" prop="status">
              {{statusFormat(form)}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="性别">
              {{sexFormat(form)}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="潜水等级" prop="diveLevel">
              {{diveLevelFormat(form)}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="潜水系统">
              {{diveSystemFormat(form)}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="头像" prop="avatar">
              <el-avatar shape="square" :size="50" :src="form.avatar"></el-avatar>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员起始时间" prop="memberBeginTime">
              {{ parseTime(form.memberBeginTime,'{y}-{m}-{d}') }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会员截止时间" prop="memberExpireTime">
              {{ parseTime(form.memberExpireTime,'{y}-{m}-{d}') }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="注册时间" prop="registerTime">
              {{ parseTime(form.registerTime) }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最后登录时间" prop="lastLoginTime">
              {{ parseTime(form.lastLoginTime) }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, exportUser , frozenUser , thawUser } from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户等级，数据字典字典
      memberLevelOptions: [],
      // 账户状态，数据字典字典
      statusOptions: [],
      // 会员状态，数据字典字典
      memberStatusOptions: [],
      // 性别，数据字典字典
      sexOptions: [],
      // 潜水等级，数据字典字典
      diveLevelOptions: [],
      diveSystemOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        memberLevel: [
          { required: true, message: "用户等级，数据字典不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "账户状态，数据字典不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "账户状态，数据字典不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "账户状态，数据字典不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("mem_member_level").then(response => {
      this.memberLevelOptions = response.data;
    });
    this.getDicts("mem_account_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("mem_member_status").then(response => {
      this.memberStatusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("mem_member_dan_level").then(response => {
      this.diveLevelOptions = response.data;
    });
    this.getDicts("mem_member_dive_system").then(response => {
      this.diveSystemOptions= response.data;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 用户等级，数据字典字典翻译
    memberLevelFormat(row, column) {
      return this.selectDictLabel(this.memberLevelOptions, row.memberLevel);
    },
    // 账户状态，数据字典字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 会员状态，数据字典字典翻译
    memberStatusFormat(row, column) {
      return this.selectDictLabel(this.memberStatusOptions, row.memberStatus);
    },
    // 性别，数据字典字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 潜水等级，数据字典字典翻译
    diveLevelFormat(row, column) {
      return this.selectDictLabel(this.diveLevelOptions, row.diveLevel);
    },
    // 潜水等级，数据字典字典翻译
    diveSystemFormat(row, column) {
      return this.selectDictLabel(this.diveSystemOptions, row.diveSystem);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        mobile1: null,
        mobile2: null,
        nickname: null,
        avatar: null,
        memberCardId: null,
        memberNum: null,
        memberLevel: null,
        lastLoginTime: null,
        registerTime: null,
        status: 0,
        memberStatus: 0,
        memberBeginTime: null,
        memberExpireTime: null,
        birthday: null,
        sex: null,
        diveLevel: null,
        email: null,
        industryId: null,
        address1: null,
        address2: null,
        country: null,
        postcode: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    handleFrozen(row){
      const ids = row.id || this.ids;
      this.$confirm('是否确认冻结用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return frozenUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("冻结成功");
        }).catch(function() {});
    },
    handleThaw(row){
      const ids = row.id || this.ids;
      this.$confirm('是否确认解冻用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return thawUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("冻结成功");
        }).catch(function() {});
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.open = true;
      this.form = row;
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
